﻿using System;
using System.Diagnostics;
using System.Threading;

#if WINDOWS
using System.Runtime.InteropServices;
#endif

namespace MicoBenchmarks
{
	class Program
	{
		#if WINDOWS
		[StructLayout(LayoutKind.Sequential)]
		public struct TimeCaps
		{
			public uint wPeriodMin;
			public uint wPeriodMax;
		}

		[DllImport("winmm.dll", EntryPoint="timeGetDevCaps", SetLastError=true)]
		public static extern uint TimeGetDevCaps(ref TimeCaps timeCaps, uint sizeTimeCaps);

		[DllImport("winmm.dll", EntryPoint="timeBeginPeriod", SetLastError=true)]
		public static extern uint TimeBeginPeriod(uint uMilliseconds);

		[DllImport("winmm.dll", EntryPoint="timeEndPeriod", SetLastError=true)]
		public static extern uint TimeEndPeriod(uint uMilliseconds);
		#endif

		static void Main(string[] args)
		{
			#if WINDOWS
			var caps = new TimeCaps();
			uint tic = TimeGetDevCaps(ref caps, (uint)Marshal.SizeOf(typeof(TimeCaps)));
			TimeBeginPeriod(caps.wPeriodMin);
			#endif

			Console.WriteLine(Stopwatch.IsHighResolution);

			Console.WriteLine("Initializing...");
			var stopwatch = new Stopwatch();
			Thread.Sleep(1000);

			Console.WriteLine("Enter a random number and hit Enter.");
			retry:;
			string number = Console.ReadLine();
			float numberValue = 0;
			if (!float.TryParse(number, out numberValue))
			{
				Console.WriteLine("Invalid number.");
				goto retry;
			}

			//Casting.RunStructTest(stopwatch, numberValue);
			Matricies.RunMultiply(stopwatch, numberValue);

			Console.WriteLine("DONE - Press Enter to exit.");
			Console.ReadLine();

			#if WINDOWS
			TimeEndPeriod(caps.wPeriodMin);
			#endif
		}
	}
}
